package wsz.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;


/**
 * 消费者
 * @author wsz
 * @date 2022/2/21 16:29
 **/
//@Component
public class MessageReceiver {

    /**
     * 消费者
     * @param message
     * @param channel
     */
    @RabbitHandler
    @RabbitListener(queues = "queue.pay")
    public void payReceiver(Message message, Channel channel) {
        System.out.println("队列准备处理数据");
        try {
            System.out.println(new String(message.getBody(), "utf-8"));
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    @RabbitListener(queues = "queue.pay.dlx")
    public void payDlxReceiver(Message message, Channel channel) {
        try {
            System.out.println("死信队列准备处理数据");
            System.out.println(new String(message.getBody(), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
